package homework_1;

import java.util.Scanner;

public class QuadraticFunctionWithOneUnknown {
    public static void main(String[] args) {
        double a,b,c;
        String input_prompt="请输入一元二次方程的系数";
        output("******一元二次方程求解程序*******" );

        do{
            output("开始求解一元二次方程，退出请输入0");
            a=input(input_prompt+"a");
            if(a==0){
                output("程序结束，再见！");
                break;
            }
            b=input(input_prompt+"b");
            c=input(input_prompt+"c");
            output(resolve(a,b,c));
            output("#########################");
        }while(true);

    }
    public static double input(String prompt){
        double result;
        Scanner scanner=new Scanner(System.in);
        output(prompt);
        result=scanner.nextDouble();
        return result;
    }
    public static double delta(double a,double b,double c){

        return b*b-4*a*c;
    }
    public static String resolve(double a, double b,double c){
        String result="该方程";
        double x1,x2,delt;
        delt=delta(a,b,c);
        if(delt>0){
            x1=(-b+Math.sqrt(delt))/(2*a);
            x2=(-b-Math.sqrt(delt))/(2*a);
            result=result+"有两个实数根，分别是"+x1+"和"+x2;
        }
        if (delt==0) {
            x1=-b/(2*a);
            result=result+"有两个相等的实数根，为"+x1;
        }
        if(delt<0){
            result=result+"没有实数根";
        }
        return result;
    }
    public static void output(String message){

        System.out.println(message);
    }

}
